<template>
	<view>
		<!-- 顶部导航 -->
		<fa-navbar :title="title"></fa-navbar>
		<u-sticky :enable="false">
			<fa-user-header :user="user" :showMore="showMore"></fa-user-header>
			<!-- #ifdef MP-BAIDU -->
			<view class="" v-if="theme.bgColor"><fa-u-tabs height="85" :list="list" :active-color="theme.bgColor" :is-scroll="false" :current="current" @change="change"></fa-u-tabs></view>
			<!-- #endif -->
			<!-- #ifndef MP-BAIDU -->
			<u-tabs :list="list" height="85" :active-color="theme.bgColor" :is-scroll="false" :current="current" @change="change"></u-tabs>
			<!-- #endif -->
		</u-sticky>
		<!-- 问题列表 -->
		<fa-question-item :type="1" :questionList="questionList"></fa-question-item>
		<!-- 空数据 -->
		<wanl-empty style="padding:0;" v-if="!questionList.length"></wanl-empty>
		<!-- 加载更多 -->
		<view class="u-p-t-10 u-p-b-30" v-if="questionList.length"><u-loadmore bg-color="#f4f6f8" :status="has_more ? status : 'nomore'" /></view>
		<!-- 回到顶部 -->
		<u-back-top :scroll-top="scrollTop" :icon-style="{color:theme.bgColor}" :custom-style="{backgroundColor:theme.lightColor}"></u-back-top>
		<!-- 底部导航 -->
		<fa-tabbar></fa-tabbar>
	</view>
</template>

<script>
import { styleRest } from '@/common/fa.mixin.js';
export default {
	mixins: [styleRest],
	onLoad(e) {
		let query = this.$Route.query || e || {};
		let user_id = query.user_id || 0;
		var lifeData = uni.getStorageSync('lifeData')
		if (user_id) {
			this.title = 'TA的提问';
			uni.setNavigationBarTitle({
				title: this.title
			});
			this.showMore = true;			
			this.user = this.vuex_othen_user;
		} else {
			this.user = lifeData.vuex_user;
		}
		this.current = e.type == 'unsolved' ? 3 : 0;
		console.log(this.user)
		this.getUserQuestion();
	},
	data() {
		return {
			title: '我的提问',
			user: {},
			list: [
				{
					name: '最新',
					text: 'new'
				},
				{
					name: '热门',
					text: 'hot'
				},
				{
					name: '悬赏',
					text: 'price'
				},
				{
					name: '未解决',
					text: 'unsolved'
				},
				{
					name: '未回答',
					text: 'unanswer'
				}
			],
			showMore: false,
			current: 0,
			scrollTop: 0,
			questionList: [],
			page: 1,
			has_more: false,
			status: 'loadmore',
			is_again: false
		};
	},
	methods: {
		change(index) {
			this.current = index;
			this.is_again = true;
			this.page = 1;
			this.getUserQuestion();
		},
		getUserQuestion: async function() {
			let res = await this.$api.getUserQuestion({
				type: this.list[this.current].text,
				id: this.user.id,
				page: this.page
			});
			this.status = 'loadmore';
			if (!res.code) {
				this.$u.toast(res.msg);
				return;
			}
			if (this.is_again) {
				this.questionList = [];
				this.is_again = false;
			}
			//分页简洁判断
			if(this.vuex_config.pagesimple.indexOf('question')!=-1){
				this.has_more = res.data.has_more;
			}else{
				this.has_more = res.data.last_page>res.data.current_page;
			}						
			res.data.data.map(this.setStyle);
			this.questionList = this.questionList.concat(res.data.data);
		}
	},
	onPageScroll(e) {
		this.scrollTop = e.scrollTop;
	},
	onReachBottom() {
		if (this.has_more) {
			this.status = 'loading';
			this.page++;
			this.getUserQuestion();
		}
	}
};
</script>

<style>
page {
	background-color: #f4f6f8;
}
</style>
